require("./index.less");
//引入字体图标
require("../../assets/font1/iconfont.css");
require("../../assets/fonts/iconfont.css");
//引入swiper
import Swiper from "../../libs/swiper/swiper-bundle.min.js";
import "../../libs/swiper/swiper-bundle.min.css";

// 引入模块
const dom = require("../../utils/dom.js");
const axios = require("axios");

document.addEventListener("DOMContentLoaded", function () {
	//swiper插件
	var mySwiper = new Swiper(".swiper", {
		autoplay: {
			delay: 1000,
			stopOnLastSlide: false,
			disableOnInteraction: false,
		},
		loop: true,
		pagination: {
			el: ".swiper-pagination",
			clickable: true,
		},
	});
	//如果.你初始化时没有定义Swiper实例，后面也可以通过Swiper的HTML元素来获取该实例
	new Swiper(".swiper");
	var mySwiper = document.querySelector(".swiper").swiper;
	mySwiper.slideNext();

	// baseURL便于为axios实例的方法传递相对 URL
	axios.defaults.baseURL = "http://fitness.h5.itsource.cn:3701";
	let token = localStorage.getItem("token");
	axios.defaults.headers.common["Authorization"] = "Bearer " + token;
	//发送请求获取用户信息
	axios.get("/api/user/info").then(function (res) {
		let data = res.data.data;
		//渲染排名
		dom.get("#rankingNum").textContent = data.ranking;
		//渲染徽章
		dom.get("#badgeNum").textContent = data.badges;
	});
	//发送请求获取打卡信息
	function renderClockin() {
		axios.get("/api/user/clockInInfo").then(function (res) {
			dom.get("#clockIndays").textContent = res.data.data.clockCount;
			let btnNode = dom.get("#clockIn");
			if (res.data.data.isClockIn) {
				//代表已经打开
				btnNode.disabled = true;
				btnNode.style.backgroundColor = "#9d9e99";
				btnNode.textContent = "已打卡";
			}
		});
	}
	renderClockin();
	//给打卡按钮绑定事件
	dom.get("#clockIn").addEventListener("click", function () {
		//发送打开请求
		axios.post("/api/user/clockIn").then(function (res) {
			if (res.data.errno === 0) {
				alert("打卡成功,明天再来哟");
				renderClockin();
			}
		});
	});
});
